﻿using Microsoft.AspNetCore.Identity;
using MvcCookieAuthSample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace MvcCookieAuthSample.Data
{
    public class ApplicationDbContextSeed
    {
        private UserManager<ApplicationUser> _userManager;
        private RoleManager<ApplicationUserRole> _roleManager;
        public async Task SeedAsync(ApplicationDbContext context, IServiceProvider services)
        {
            if (!context.Roles.Any())
            {
                _roleManager = services.GetRequiredService<RoleManager<ApplicationUserRole>>();
                var role = new ApplicationUserRole() { Name = "Administrators", NormalizedName = "Administrators" };
                var result = await _roleManager.CreateAsync(role);

                if (!result.Succeeded)
                {
                    throw new Exception("初始默认角色失败" + result.Errors.SelectMany(e => e.Description));
                }
            }
            if (!context.Users.Any())
            {
                _userManager = services.GetRequiredService<UserManager<ApplicationUser>>();

                var defaultUser = new ApplicationUser
                {
                    UserName = "Administrator",
                    Email = "admin@qq.com",
                    NormalizedUserName = "admin",
                    Avatar = "http://localhost:5000/meizhi.jpg"
                };

                await _userManager.AddToRoleAsync(defaultUser, "Administrators");

                var result = await _userManager.CreateAsync(defaultUser, "Password$123");

                if (!result.Succeeded)
                {
                    throw new Exception("初始默认用户失败");
                }
            }
        }
    }
}
